<?php

declare(strict_types=1);

use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;

if (! function_exists('getUserAgent')) {
    /**
     * 获取指定user-agent里的值
     *
     * @param string $userAgent
     * @param string $specifyValue
     * @return bool
     */
    function getUserAgent(string $userAgent, string $specifyValue = ''): bool
    {
        if (empty($specifyValue)) {
            return false;
        }

        return strripos($userAgent, $specifyValue) !== false;
    }    
}

if (! function_exists('getNamespace')) {
    /**
     * 获取处理后的命名空间路径值
     *
     * @param $name
     * @return array|string|string[]
     */
    function getNamespace(string $name): string
    {
        $value = ucwords(str_replace(['-', '_'], ' ', $name));
        return str_replace(' ', '', $value);
    }
}

if (! function_exists('checkPhone')) {
    /**
     * 手机号是否合法
     * @param string $phone
     * @param bool $throw
     * @return bool
     * @throws ParamsException
     */
    function checkPhone(string $phone = "", bool $throw = false): bool
    {
        $reg = "/^1[3456789]\d{9}$/";
        $r = (bool)preg_match($reg, $phone);

        if (!$r) {
            //手机号特殊格式处理，例：136******66

            if (substr($phone, 3, 6) !== '******') {
                throw new CommonException(ErrorCode::INVALID_PARAMS, '手机号格式非法');
            }

            $phonePrefix = preg_match('/^1[3456789]\d{1}$/', substr($phone, 0, 3));
            if (!$phonePrefix) {
                throw new CommonException(ErrorCode::INVALID_PARAMS, '手机号格式非法');
            }

            $phoneSuffix = strlen(substr($phone, 9, 2));
            if ($phoneSuffix != 2 || !is_numeric($phoneSuffix)) {
                throw new CommonException(ErrorCode::INVALID_PARAMS, '手机号格式非法');
            }

            return true;
        }

        if ($throw && !$r) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '手机号格式非法');
        }

        return $r;
    }
}

if (! function_exists('hiddenPhone')) {
    /**
     * 手机号中间位数隐藏处理
     *
     * @param string $phone
     * @param int $start
     * @param int $len
     * @return array|string|string[]
     */
    function hiddenPhone(string $phone = '', int $start = 3, int $len = 6)
    {
        if (empty($phone) || strlen($phone) != 11) {
            return $phone;
        }

        return substr_replace($phone, '******', $start, $len);
    }
}

if (! function_exists('serviceMap')) {
    /**
     * 模型服务与契约的依赖配置.
     * @param string $path 契约与服务的相对路径
     * @return array 依赖数据
     */
    function serviceMap(string $path, string $namespacePrefix): array
    {
        $namespacePrefix = ltrim($namespacePrefix, '\\');
        $services = readFileName($path . '/Service');

        $dependencies = [];
        foreach ($services as $service) {
            $contractFilename = str_replace('Service', 'Contract', $service);
            $dependencies[$namespacePrefix . '\\Contract\\' . $contractFilename] = $namespacePrefix . '\\Service\\' . $service;
        }

        return $dependencies;
    }
}

if (! function_exists('registerServiceMap')) {
    /**
     * 模型服务与契约的依赖配置.
     * @param string $path 契约与服务的相对路径
     * @return array 依赖数据
     */
    function registerServiceMap(string $path, string $namespacePrefix = 'WanZ\\App\\'): array
    {
        $dependencies = [];
        if (! is_dir($path)) {
            return $dependencies;
        }

        $files = scandir($path);
        foreach ($files as $file) {
            if (in_array($file, ['.', '..', '.DS_Store'])) {
                continue;
            }

            if (! is_dir($path . '/' . $file . '/src')) {
                continue;
            }
            $arr = explode('-', $file);
            array_walk($arr, function (&$val) {
                $val = ucfirst($val);
            });
            $namespace = $namespacePrefix . implode('', $arr);
            $dependencies = array_merge(serviceMap($path . '/' . $file . '/src', $namespace), $dependencies);
        }
        return $dependencies;
    }
}
